package com.frossini.proyectologin.adapter;

import java.io.File;
import java.text.SimpleDateFormat;

import com.frossini.proyectologin.R;
import com.frossini.proyectologin.model.Tarea;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TareasAdapter extends ArrayAdapter<Tarea>
{
	private SimpleDateFormat df = new SimpleDateFormat("EEEE dd 'de' MMM 'de' yyyy '-' hh:mm");
	private class ViewHolder
	{
		TextView tv_tli_fecha;
		TextView tv_tli_descripcion;
		TextView tv_id_tarea ;
		ImageView foto;
	}
	
	public TareasAdapter(Context context){
		super(context,-1);
	
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		Tarea tarea = getItem(position); 
		ViewHolder viewHolder;
		
		if (convertView ==null)
		{
			LayoutInflater inflater = LayoutInflater.from(getContext());
			convertView = inflater.inflate(R.layout.tarea_list_item,parent,false);
			viewHolder = new ViewHolder();
			viewHolder.tv_id_tarea = (TextView) convertView.findViewById(R.id.tv_tli_id);
			viewHolder.tv_tli_fecha = (TextView) convertView.findViewById(R.id.tv_tli_fecha);
			viewHolder.tv_tli_descripcion = (TextView) convertView.findViewById(R.id.tv_tli_descripcion);
			viewHolder.foto = (ImageView) convertView.findViewById(R.id.foto);
			convertView.setTag(viewHolder);
		
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		
		viewHolder.tv_id_tarea.setText("Cod.: (" + String.valueOf(tarea.getIdTarea() + ")"));
		viewHolder.tv_id_tarea.setTextColor(Color.parseColor("#00008B"));
		viewHolder.tv_tli_fecha.setText(df.format(tarea.getFechaTarea()));
		viewHolder.tv_tli_descripcion.setText(tarea.getDescripcion());
		String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/" + "_1437430237900.jpg";
		File f = new File(dir); 
		ImageLoader.getInstance().displayImage("file://"+f.getAbsolutePath(), viewHolder.foto);
		
		 if(position % 2 == 0)
		 {  
			 convertView.setBackgroundColor(Color.rgb(238, 233, 233));
			   }
			   else 
			   {
				   convertView.setBackgroundColor(Color.rgb(255, 255, 255));
			   }
		 
			 if (tarea.getEnvioMail()== true ) 
				{
					convertView.setBackgroundColor(Color.rgb(255, 255, 102));
				} 
				else 
				{
	
				}
		 
			 if (tarea.getIdTarea()== 0 ) 
				{
					//convertView.setBackgroundColor(Color.parseColor("#96FD96"));
				} else
				{
					convertView.setBackgroundColor(Color.parseColor("#96FD96"));
				}
			 return convertView;
	}
	

}
